package top.wangjiu.reactive.rpc.registration;

import top.wangjiu.reactive.rpc.model.RpcService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName LocalServerRegister
 * @Description TODO
 * @Author wangjiu
 * @Date 2021/7/15 7:27 下午
 */
public class LocalServerRegister implements ServerRegister{

    private final List<RpcService> serviceList = new ArrayList<>();
    private final Map<String,RpcService> serviceMap = new ConcurrentHashMap<>();

    @Override
    public <T> void registService(Class<T> service,int version,T instance) {
        RpcService rpcService = new RpcService(service.getName(), service, version, instance);
        serviceList.add(rpcService);
        serviceMap.put(getServiceId(service.getName(),version),rpcService);
    }

    @Override
    public RpcService getService(String service,int version) {
        return serviceMap.get(getServiceId(service,version));
    }

    private String getServiceId(String service,int version){
        return service+":"+version;
    }

}
